module cpara
import StdEnv, japi

/* This is another AP exercise. You must find the
path of a cannonball, given the initical velocity.
********************************** */

:: Stt = {   fr :: !Framer, cv :: !RC, 
             mouse :: !Device, 
             x::!Real, y::!Real,
             vx::!Real, vy::!Real,
             xs::![(Real, Real)]}

Start world
	#	(js, world) = begin_japi world
		(frm, js) = jnewFrame (WH 500 530) "Short" js
		(cvs, js) = jnewCanvas (XYWH 0 30 500 500) frm js
		(mp, cvs) = jmousepressed cvs 
		(frm, js) = jshow frm js
		rcv= mks cvs (0.0, 0.0) (500.0, 500.0)
		s = { fr = frm, cv = rcv, mouse = mp, 
		      x=0.0, y=0.0, vx=20.0, vy=80.0, xs=[]}
		js = loop s js
	= end_japi js world
where
	loop :: !Stt  !JS -> JS
	loop s js 
		# (x, js)= jsleep 250 js	
		# (na, js) = jgetaction x js
		| na == act s.fr = jquit js
		| na == act s.mouse
		     # (ix, iy) = mxy s.mouse
		     # ((vx0, vy0), rcv)= rmxy s.mouse s.cv
		     # rcv= draw [ Background white,
		                   Arrow (0.0, 0.0) (vx0, vy0)]  rcv
		     = loop {s & cv=rcv, xs= [(0.0, 0.0)], x= 0.0, 
		                 y= 0.0, vx=vx0, vy= vy0} js
		| s.y >= 0.0 && s.xs <> []
			# 	rcv= draw [ Color white,
		                    Circle (hd s.xs) 10.0,
		                    Color red,
		                    Circle (s.x, s.y) 10.0,
		                    Circle (s.x, s.y) 2.0] s.cv
		     	(ux, uy)= (s.vx, s.vy - 0.5*10.0)
			=  	loop {s & x= s.x + ux * 0.5,
		            y= s.y + uy * 0.5,
		            xs= [(s.x, s.y):s.xs],
		            vx= ux, vy=uy, cv=rcv} js 
		# msg= "Click the mouse inside the blue circle."
		  rcv= draw [  Color black,
		               Text (100.0, 40.0) msg,
		               Color blue, 
		               Circle (0.0, 0.0) 90.0] s.cv
		= loop {s & cv= rcv} js
		
	
	